﻿// Project: Salient
// http://salient.codeplex.com
// 
// Copyright 2010, Sky Sanders <sky at skysanders.net>
// Dual licensed under the MIT or GPL Version 2 licenses.
// http://salient.codeplex.com/license
// 
// Date: May 5 2010 

#region

using System;
using System.Configuration.Provider;
using System.IO;

#endregion

namespace Salient.Web.Util
{
    ///<summary>
    ///</summary>
    public static class ProviderBaseLogWriter
    {
        private static readonly string LogFormat;
        private static string _errorTime;

        static ProviderBaseLogWriter()
        {
            LogFormat = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + " ==> ";

            StartNewLog();
        }

        ///<summary>
        ///</summary>
        public static void StartNewLog()
        {
            string sYear = DateTime.Now.Year.ToString();
            string sMonth = DateTime.Now.Month.ToString();
            string sDay = DateTime.Now.Day.ToString();
            _errorTime = sYear + sMonth + sDay;
        }

        ///<summary>
        ///</summary>
        ///<param name="provider"></param>
        ///<param name="ex"></param>
        ///<typeparam name="T"></typeparam>
        public static void LogError<T>(this T provider, Exception ex) where T : ProviderBase
        {
            try
            {
                StreamWriter sw = new StreamWriter(provider.Name + "." + _errorTime + ".log", true);
                sw.WriteLine(LogFormat + ex.Message);
                sw.Flush();
                sw.Close();
            }
                // ReSharper disable EmptyGeneralCatchClause
            catch
                // ReSharper restore EmptyGeneralCatchClause
            {
            }
        }
    }
}